package org.grow.auth.controller;

import org.grow.auth.dao.UserInfoMapper;
import org.grow.auth.entity.UserInfo;
import org.grow.dto.PagedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Author: xwg
 * @CreateDate: 2022/5/26
 */

@RestController
@RequestMapping("/auth/userInfo")
public class UserInfoController {
    @GetMapping("/currentUser")
    public String getCurrentUser(HttpServletRequest request) {
        System.out.println("获取当前用户:");
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(name);
        return name;
    }

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @PostMapping("/insert")
    public UserInfo insert(@RequestBody UserInfo userInfo) {

        userInfo.setUserId(-1);
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        userInfo.setLastLoginTime(null);
        userInfo.setEnabled(true);
        userInfoMapper.insert(userInfo);
        return userInfo;
    }

    @GetMapping("/testCommonDto")
    public PagedResult<String> testCommonDto() {
        return new PagedResult<String>(10L, Arrays.asList("zhang", "san"));
    }
}
